/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.Enum.ActivityStatusEnum;
import com.ykht.proj.activity.Enum.AgreementStatusEnum;
import com.ykht.proj.activity.Enum.PlanStatusEnum;
import com.ykht.proj.activity.constant.CardConstant;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.handle.OrderHandler;
import com.ykht.proj.activity.mapper.VCardMapper;
import com.ykht.proj.activity.service.*;
import com.ykht.proj.activity.vo.*;
import com.ykht.proj.common.core.constant.CommonConstants;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.api.feign.RemoteLabelService;
import com.ykht.proj.coupon.api.feign.RemoteShopService;
import com.ykht.proj.coupon.entity.CnLabel;
import com.ykht.proj.coupon.vo.BaseLonLat;
import com.ykht.proj.coupon.vo.CardLabVo;
import com.ykht.proj.coupon.vo.CnShopDisVo;
import com.ykht.proj.coupon.vo.ShopDisListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 卡表
 *
 * @author csg
 * @date 2021-05-21 14:21:47
 */
@Slf4j
@Service
public class VCardServiceImpl extends ServiceImpl<VCardMapper, VCard> implements VCardService {
	@Resource
	private VCardMapper vCardMapper;
	@Resource
	private VCardLabService vCardLabService;
	@Resource
	private VCardGroupService vCardGroupService;
	@Resource
	private RemoteShopService remoteShopService;
	@Resource
	private RemoteLabelService remoteLabelService;
	@Resource
	private VDiscountPlanService vDiscountPlanService;
	@Resource
	private VCardService vCardService;
	@Autowired
	private OrderHandler orderHandler;

	/**
	 * 添加卡
	 *
	 * @param vCardVo
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addCard(VCardVo vCardVo) {
		Long userId = SecurityUtils.getUserId();
		vCardVo.setCreateUser(userId);
		vCardVo.setCreateTime(LocalDateTime.now());
		VCard card = BeanUtil.toBean(vCardVo, VCard.class);
		this.save(card);
		// 创建标签 数据
		List<VCardLab> cardLabList = new ArrayList<>();
		vCardVo.getLabelIds().stream().forEach(itme -> {
			VCardLab cardLab = new VCardLab();
			cardLab.setLabelId(itme);
			cardLab.setCardId(card.getId());
			cardLab.setCreateUser(userId);
			cardLab.setCreateTime(LocalDateTime.now());
			cardLabList.add(cardLab);
		});
		vCardLabService.saveBatch(cardLabList);
		// 创建标签 群组 数据
		List<VCardGroup> cardGroupList = new ArrayList<>();
		vCardVo.getGroupIds().stream().forEach(itme -> {
			VCardGroup cardGroup = new VCardGroup();
			cardGroup.setGroupId(itme);
			cardGroup.setCardId(card.getId());
			cardGroup.setCreateUser(userId);
			cardGroup.setCreateTime(LocalDateTime.now());
			cardGroupList.add(cardGroup);
		});
		vCardGroupService.saveBatch(cardGroupList);
	}

	/**
	 * 修改卡片
	 *
	 * @param vCardVo
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateCard(VCardVo vCardVo) {
		Long userId = SecurityUtils.getUserId();
		vCardVo.setUpdateUser(userId);
		vCardVo.setUpdateTime(LocalDateTime.now());
		VCard card = BeanUtil.toBean(vCardVo, VCard.class);
		this.updateById(card);
		// 创建标签 数据
		List<VCardLab> cardLabList = new ArrayList<>();
		vCardVo.getLabelIds().stream().forEach(itme -> {
			VCardLab cardLab = new VCardLab();
			cardLab.setLabelId(itme);
			cardLab.setCardId(card.getId());
			cardLab.setCreateUser(userId);
			cardLab.setCreateTime(LocalDateTime.now());
			cardLabList.add(cardLab);
		});
		vCardLabService.remove(Wrappers.lambdaQuery(VCardLab.class).eq(VCardLab::getCardId, card.getId()));
		vCardLabService.saveBatch(cardLabList);
		// 创建标签 群组 数据
		List<VCardGroup> cardGroupList = new ArrayList<>();
		vCardVo.getGroupIds().stream().forEach(itme -> {
			VCardGroup cardGroup = new VCardGroup();
			cardGroup.setGroupId(itme);
			cardGroup.setCardId(card.getId());
			cardGroup.setCreateUser(userId);
			cardGroup.setCreateTime(LocalDateTime.now());
			cardGroupList.add(cardGroup);
		});
		vCardGroupService.remove(Wrappers.lambdaQuery(VCardGroup.class).eq(VCardGroup::getCardId, card.getId()));
		vCardGroupService.saveBatch(cardGroupList);
	}

	@Override
	public List<CnLabel> cardLabel(Long cardId) {
		List<Long> lableList = new VCardLab().selectList(Wrappers.lambdaQuery(VCardLab.class).eq(VCardLab::getCardId, cardId)).stream().map(VCardLab::getLabelId).collect(Collectors.toList());
		R<List<CnLabel>> r = remoteLabelService.list(lableList);
		List<CnLabel> labels = new ArrayList<>();
		if (r.getCode() == CommonConstants.SUCCESS) labels = r.getData();
		return labels;
	}

	@Override
	public List<VCardInfo.ShopVo> shopVoListByCardId(BaseLonLat lonLat, Long cardId) {
		return this.shopVoListByVId(lonLat, null, cardId);
	}

	@Override
	public List<VCardInfo.ShopVo> shopVoListByVId(BaseLonLat lonLat, Long vId, Long cardId) {
		List<VCardInfo.ShopVo> shopVoList = new ArrayList<>();
		VCard card = new VCard().selectById(cardId);
		VInfo info = new VInfo().selectById(vId);

		List<Long> shopIds;
		if (vId != null && card.getDiscountType() == CardConstant.DISCOUNT_TYPE_BF) {
			List<VOrderCardShop> vOrderCardShopList = new VOrderCardShop().selectList(Wrappers.lambdaQuery(VOrderCardShop.class).select(VOrderCardShop::getShopId).eq(VOrderCardShop::getOrderCardId, info.getOrderCardId()));
			shopIds = vOrderCardShopList.stream().map(VOrderCardShop::getShopId).collect(Collectors.toList());
		} else {
			List<Long> groups = new VCardGroup().selectList(Wrappers.lambdaQuery(VCardGroup.class).eq(VCardGroup::getCardId, cardId)).stream().map(VCardGroup::getGroupId).collect(Collectors.toList());
			log.info("--->获取可选门店");
			if (groups.size() == 0) return shopVoList;
			shopIds = new VGroupShop().selectList(Wrappers.lambdaQuery(VGroupShop.class).select(VGroupShop::getShopId).in(VGroupShop::getGroupId, groups)).stream().map(VGroupShop::getShopId).collect(Collectors.toList());
		}
		shopVoList = this.getShopDisVo(lonLat, shopIds, card);
		//不用计算次数，返回
		if (vId == null) return shopVoList;
		//核销的数据
		List<VConsumeRecord> useList = new VConsumeRecord().selectList(Wrappers.lambdaQuery(VConsumeRecord.class).eq(VConsumeRecord::getInfoId, vId));

		shopVoList = shopVoList.stream().map(o -> {
			VDiscountPlan plan = o.getPlan();
			//使用次数类型 0 不限制次数 1限制次数
			if (plan.getUsageType() == 1) {
				o.setAllNum(plan.getUsageTimes());
				Integer useNum = 0;
				//计算类型 0 门店使用计算 1品牌使用计算
				switch (plan.getCalculationType()) {
					case 0:
						useNum = (int) (useList.stream().filter(oo -> o.getShopId().equals(oo.getShopId())).count());
						break;
					case 1:
						useNum = (int) (useList.stream().filter(oo -> o.getBrandId().equals(oo.getBrandId())).count());
						break;
				}
				o.setUseNum(useNum);
			}
			return o;
		}).collect(Collectors.toList());
		return shopVoList;
	}


	@Override
	public Page cardPage(Page page, VCard vCard, BaseLonLat baseLonLat) {

		//所有的门店
//		List<CnShopDisVo> disList = remoteShopService.distanceList(baseLonLat);

		//所有的卡
		//距离排序
		return null;
	}

	@Override
	public VCardInfo4PC getCardInfo4PC(Long id) {
		VCardInfo4PC info = new VCardInfo4PC();
		//卡
		log.info("--->获取卡信息");
		info.setCard(this.getById(id));
		//卡标签
		log.info("--->获取卡标签");
		List<CnLabel> labels = this.cardLabel(id);
		info.setLabel(String.join("·", labels.stream().map(CnLabel::getName).collect(Collectors.toList())));
		//可选门店
		log.info("--->获取可选门店");
		List<VCardInfo.ShopVo> shopVoList = this.shopVoListByCardId(null, id);
		List<VCardInfo4PC.ShopVo> voList = shopVoList.stream().map(o -> BeanUtil.toBean(o, VCardInfo4PC.ShopVo.class)).collect(Collectors.toList());
		info.setShopList(voList);
		return info;
	}
	/**
	 * 卡详情
	 *
	 * @param id
	 * @param lonLat
	 * @return
	 */
	@Override
	public VCardInfo getCardInfo(Long id, BaseLonLat lonLat) {
		VCardInfo info = new VCardInfo();
		//卡
		log.info("--->获取卡信息");
		info.setCard(this.getById(id));
		//活动
		log.info("--->获取活动");
		VActivityCard activityCard = new VActivityCard().selectOne(Wrappers.lambdaQuery(VActivityCard.class).eq(VActivityCard::getCardId, id).eq(VActivityCard::getStatus, ActivityStatusEnum.AT_WORK.getValue()));
		if (activityCard != null) {
			VActivity vActivity = new VActivity().selectOne(Wrappers.lambdaQuery(VActivity.class).eq(VActivity::getId, activityCard.getActivityId()).eq(VActivity::getStatus, 0));
			info.setActivity(vActivity);
			info.setActivityCard(activityCard);
		}
		//是否可购买
		info.setCanBuy(orderHandler.checkCanBuy(id, null));
		//卡标签
		log.info("--->获取卡标签");
		List<CnLabel> labels = this.cardLabel(id);
		info.setLabel(String.join("·", labels.stream().map(CnLabel::getName).collect(Collectors.toList())));
		//可选门店
		log.info("--->获取可选门店");
		List<VCardInfo.ShopVo> shopVoList = this.shopVoListByCardId(lonLat, id);
		info.setShopList(shopVoList);
		return info;
	}

	//转换数据
	public List<VCardInfo.ShopVo> getShopDisVo(BaseLonLat lonLat, List<Long> shopIds, VCard card) {
		log.info("--->获取远程门店的相关信息");
		List<VCardInfo.ShopVo> shopVoList = new ArrayList<>();

		R<List<CnShopDisVo>> r = remoteShopService.distanceList(new ShopDisListVo(lonLat, shopIds));
		List<CnShopDisVo> shopList = r.getData();
		if (shopList.size() > 0) {
			VPlanInfoReq vPlanInfoReq = new VPlanInfoReq();
			vPlanInfoReq.setShopIds(shopIds);
			vPlanInfoReq.getBa().setAgreementStatus(AgreementStatusEnum.AT_WORK.getValue());//协议状态 0 进行中 1待开始 2 暂停中 3 已过期
			vPlanInfoReq.getDp().setPlanStatus(PlanStatusEnum.AT_WORK.getValue());//方案状态 0 进行中 1待开始 2 已结束
			//门店为维度的所有的优惠方案
			log.info("--->获取门店为维度的所有的优惠方案");
			List<VPlanInfo> vPlanInfoList = vDiscountPlanService.getPlanInfo(vPlanInfoReq);
			//组装数据
			shopVoList = shopList.stream().map(o -> {
				VCardInfo.ShopVo shopVo = BeanUtil.toBean(o,VCardInfo.ShopVo.class);
				shopVo.setBrandId(o.getBrandId());
				shopVo.setDistance(o.getDistance());
				shopVo.setLabel(o.getBrandLabel() == null ? null : o.getBrandLabel().getLabelName());
				shopVo.setLogo(o.getShopLogo());
				shopVo.setShopId(o.getId());
				shopVo.setShopName(o.getName());
				shopVo.setTradeArea(o.getTradeArea());
				switch (card.getPriceType()) {
					case 2:
						//取优
						vPlanInfoList.stream().filter(oo -> oo.getDps().getShopId().equals(o.getId()))
								.sorted(Comparator.comparing(oo -> oo.getDp().getDiscount())).findFirst().ifPresent(oo -> shopVo.setPlan(oo.getDp()));
						break;
					default:
						//门店一至并且卡的价格类型一直
						vPlanInfoList.stream().filter(oo -> oo.getDps().getShopId().equals(o.getId()) && oo.getDp().getPlanType().equals(card.getPriceType()))
								.findFirst().ifPresent(oo -> shopVo.setPlan(oo.getDp()));
						break;
				}
				return shopVo;
			}).filter(o -> null != o.getPlan().getId()).collect(Collectors.toList());
		}
		return shopVoList;
	}

	/**
	 * 获取创建活动的 卡列表分页查询
	 *
	 * @param page    分页
	 * @param vCardVo 条件
	 * @return
	 */
	@Override
	public IPage<VCardVo> getByActivityAndCardPage(Page page, VCardVo vCardVo) {
		return vCardMapper.getByActivityAndCardPage(page, vCardVo);
	}

	/**
	 * 获取企业购买卡 列表分页查询
	 *
	 * @param page    分页
	 * @param vCardVo 条件
	 * @return
	 */
	@Override
	public IPage<VCardVo> getEntBuyCardPage(Page page, VCardVo vCardVo) {
		return vCardMapper.getEntBuyCardPage(page, vCardVo);
	}

	/**
	 * 批量修改 卡库存
	 *
	 * @param cardList
	 */
	@Override
	public void updateBatch(List<VCard> cardList) {
		vCardMapper.updateBatch(cardList);
	}

	@Override
	public List<Card4C> cardList(Card4CReq req, BaseLonLat lonLat) {
		R<List<CnShopDisVo>> r = remoteShopService.distanceList(new ShopDisListVo(lonLat, null));
		if (r.getCode() != CommonConstants.SUCCESS) return new ArrayList<>();

		List<CnShopDisVo> shopDisVoList = r.getData();
		List<VCard> cardList;
		List<VActivity> activityList = new ArrayList<>();
		List<VActivityCard> activityCardList;
		//所有的卡id
		List<Long> cardIds;
		if (req.getActivityId() != null) {
			activityCardList = new VActivityCard().selectList(Wrappers.lambdaQuery(VActivityCard.class).eq(VActivityCard::getActivityId, req.getActivityId()));
			cardIds = activityCardList.stream().map(VActivityCard::getCardId).collect(Collectors.toList());
			if (cardIds.size() == 0) return new ArrayList<>();
			cardList = new VCard().selectList(Wrappers.lambdaQuery(VCard.class).in(VCard::getId, cardIds));
		} else {
			cardList = new VCard().selectList(Wrappers.lambdaQuery(VCard.class).eq(VCard::getCardStatus, 0).eq(req.getFxFlag()!=null,VCard::getFxFlag,req.getFxFlag()));
			cardIds = cardList.stream().map(VCard::getId).collect(Collectors.toList());
			if (cardIds.size() == 0) return new ArrayList<>();
			//所有的活动
			activityCardList = new VActivityCard().selectList(Wrappers.lambdaQuery(VActivityCard.class).in(VActivityCard::getCardId, cardIds).eq(VActivityCard::getStatus, ActivityStatusEnum.AT_WORK.getValue()));

		}

		log.info("************************{}",cardIds);
		//活动
		List<Long> actIds = activityCardList.stream().map(VActivityCard::getActivityId).distinct().collect(Collectors.toList());
		if (actIds.size() > 0)
			activityList = new VActivity().selectList(Wrappers.lambdaQuery(VActivity.class).in(VActivity::getId, actIds));

		//所有卡关联的门店
		List<CardShopVo> cardShopVoList = this.shopByCardIds(cardIds);
		//所有卡里的所有门店
		List<CardPlanInfo> planInfoList = vDiscountPlanService.getCardPlanInfo(cardShopVoList, shopDisVoList);

		Map<Long, CardPlanInfo> planInfoMap = planInfoList.stream().collect(Collectors.toMap(o -> o.getCard().getId(), Function.identity(), (key1, key2) -> key2));
		System.out.println(JSONUtil.toJsonStr(planInfoMap));
		//所有的卡标签
		List<VCardLab> lableList = new VCardLab().selectList(Wrappers.lambdaQuery(VCardLab.class).in(VCardLab::getCardId, cardIds));
		List<CardLabVo> labels = lableList.stream().map(o -> new CardLabVo(o.getCardId(), o.getLabelId(), null)).collect(Collectors.toList());
		R<List<CardLabVo>> r1 = remoteLabelService.listByCardLab(labels);
		if (r1.getCode() == CommonConstants.SUCCESS) labels = r1.getData();
		List<CardLabVo> finalLabels = labels;
		List<VActivityCard> finalActivityCardList = activityCardList;
		List<VActivity> finalActivityList = activityList;

		//组装返回数据
		List<Card4C> card4CList = cardList.stream().map(o -> {
			Card4C c = new Card4C();
			//卡标签
			List<String> labelName = finalLabels.stream().filter(label -> label.getCardId().equals(o.getId())).map(CardLabVo::getLabelName).limit(3).collect(Collectors.toList());
			c.setLabelName(labelName);
			//卡
			c.setCard(o);
			//折扣
			CardPlanInfo cardPlanInfo = planInfoMap.get(o.getId());
			cardPlanInfo.getShopPlanList().stream()
					.filter(oo -> oo != null && oo.getInfo() != null && oo.getInfo().getDps() != null && oo.getInfo().getDps().getDiscountPlanId() != null)
					.map(oo -> oo.getInfo().getDp().getDiscount())
					.min(BigDecimal::compareTo)//最小值
					.ifPresent(oo -> {
						c.setDiscount(oo);
					});
			//商圈
			String sq = cardPlanInfo.getShopPlanList().stream()
					.filter(oo -> oo.getShopInfo() != null)
					.sorted(Comparator.comparing(oo -> oo.getShopInfo().getDistance(), Comparator.nullsLast(Comparator.reverseOrder())))//距离倒序
					.map(oo -> oo.getShopInfo().getTradeArea())//商圈
					.distinct()//去重
					.filter(oo -> oo != null)
					.limit(3)//去三个
					.collect(Collectors.joining("、"));//字符串拼接
			//商圈
			c.setSq(sq);
			cardPlanInfo.getShopPlanList().stream()
					.filter(oo -> oo.getShopInfo().getDistance() != null)
					.mapToLong(oo -> oo.getShopInfo().getDistance())
					.findFirst()
					.ifPresent(oo -> {
						c.setDistance(oo);
					});

			//活动存在时，售卡的百分比
			//活动卡价格
			finalActivityCardList.stream().filter(oo -> oo.getCardId().equals(o.getId())).findFirst().ifPresent(oo -> {
				//活动
				finalActivityList.stream().filter(ooo -> ooo.getId().equals(oo.getActivityId())).findFirst().ifPresent(ooo -> {
					c.setActivity(ooo);
				});
				c.setActivityCard(oo);
				BigDecimal up = BigDecimal.valueOf((oo.getFalsePurchaseNumber() + oo.getSaleVolume()));
				BigDecimal dowm = BigDecimal.valueOf(oo.getActivityStock() + oo.getFalsePurchaseNumber());
				System.out.println(up);
				System.out.println(dowm);
				//四舍五入保留两位小数
				BigDecimal present = up.divide(dowm, 2, BigDecimal.ROUND_HALF_UP);
				//百分比
				c.setPercent(present);
			});
			return c;
		}).filter(o -> o.getDiscount() != null).sorted(Comparator.comparing(Card4C::getDistance, Comparator.nullsLast(Long::compareTo))//距离升序
				.thenComparing(o -> o.getActivity().getId(), Comparator.nullsLast(Comparator.reverseOrder()))//活动存在
				.thenComparing(o -> o.getActivityCard().getSaleVolume(), Comparator.nullsLast(Comparator.reverseOrder()))//销量倒叙
				.thenComparing(o -> o.getCard().getSaleVolume(), Comparator.nullsLast(Comparator.reverseOrder())))//卡销量倒叙
				.collect(Collectors.toList());
		return card4CList;
	}

	/*	public static void main(String[] args) {


			List<Card4C> list = new ArrayList<>();

	//********************************
			VActivity activity1 = new VActivity();
			activity1.setId(1l);
			VActivityCard activityCard1 = new VActivityCard();
			activityCard1.setSaleVolume(1);
			VCard card1 = new VCard();
			card1.setSaleVolume(2);
	//********************************
			Card4C c1 = new Card4C();
			c1.setActivity(activity1);
			c1.setDistance(1l);
			c1.setActivityCard(activityCard1);
			c1.setCard(card1);
	//********************************

			Card4C c2 = new Card4C();

			VActivity activity2 = new VActivity();
			activity2.setId(2l);
			VActivityCard activityCard2 = new VActivityCard();
			activityCard2.setSaleVolume(2);
			VCard card2 = new VCard();
			card2.setSaleVolume(1);
	//********************************

			c2.setActivity(activity2);
			c2.setDistance(2l);
			c2.setActivityCard(activityCard2);
			c2.setCard(card2);
	//********************************
			list.add(c1);
			list.add(c2);
			list = list.stream().sorted(Comparator.comparing(Card4C::getDistance,Comparator.nullsLast(Long::compareTo))//距离升序
					.thenComparing(o->o.getActivity().getId()!=null,Comparator.nullsLast(Comparator.reverseOrder()))//活动存在
					.thenComparing(o->o.getActivityCard().getSaleVolume(),Comparator.nullsLast(Comparator.reverseOrder()))
					.thenComparing(o->o.getCard().getSaleVolume(),Comparator.nullsLast(Comparator.reverseOrder())))
					.collect(Collectors.toList());
			System.out.println(JSONUtil.toJsonStr(list));

		}*/
	@Override
	public List<CardShopVo> shopByCardIds(List<Long> cardIds) {
		List<CardShopVo> list = vCardMapper.shopByCardIds(cardIds);
		return list;
	}

	@Override
	public ActivityInfo getActivityInfo(Card4CReq req, BaseLonLat lonLat) {
		ActivityInfo activityInfo = new ActivityInfo();
		VActivity activity = new VActivity().selectById(req.getActivityId());
		activityInfo.setActivity(activity);
		List<Card4C> list = this.cardList(req, lonLat);
		activityInfo.setList(list);
		return activityInfo;
	}

	/**
	 * 企业绑定卡  给卡添加激活数量+1
	 *
	 * @param id
	 */
	@Override
	public void updateActivationVolume(Long id) {
		vCardMapper.updateActivationVolume(id);
	}

	/**
	 * 添加库存（加减库存）
	 *
	 * @param vCardVo
	 */
	@Override
	public void addStockNumber(VCardVo vCardVo) {
		vCardMapper.addStockNumber(vCardVo);
	}

	@Override
	public R UpdateFxFlag(Long id, Integer fxFlag, BigDecimal returnPercentage) {
		VCard vCard = new VCard();
		vCard.setId(id);
		vCard.setFxFlag(fxFlag);
		vCard.setReturnPercentage(returnPercentage);
		if (0 == vCard.getFxFlag()) {
			vCard.setReturnPercentage(BigDecimal.ZERO);
		}
		vCardService.updateById(vCard);
		return R.ok();

	}

}




